package me.xplabs.model 
{
	import me.xplabs.Globals;
	import me.xplabs.model.combatunit.BaseBuff;
	import me.xplabs.model.combatunit.BaseBuild;
	import me.xplabs.model.combatunit.BaseFlag;
	import me.xplabs.model.combatunit.BaseRole;
	import me.xplabs.model.combatunit.RaidButtonData;
	import me.xplabs.model.combatunit.RaidLockData;
	import me.xplabs.model.skill.BaseSkill;
	/**
	 * http://www.xplabs.me/
	 * @author xiaohan
	 */
	public class GameDataSingleton 
	{
		private static var _instance:GameDataSingleton;
		private var _builds:Vector.<BaseBuild>;
		private var _skills:Vector.<BaseSkill>;
		private var _flags:Vector.<BaseFlag>;
		private var _energys:Vector.<BaseRole>;
		private var _buffs:Vector.<BaseBuff>;
		
		public var raidButtons:Vector.<RaidButtonData>;
		public var raidLocks:Vector.<RaidLockData>;
		
		public var raidId:int;
		
		public function GameDataSingleton() 
		{
			_builds = new Vector.<BaseBuild>();
			_skills = new Vector.<BaseSkill>();
			_energys = new Vector.<BaseRole>();
			_flags = new Vector.<BaseFlag>();
			_buffs = new Vector.<BaseBuff>();
			raidButtons = new Vector.<RaidButtonData>();
			raidLocks = new Vector.<RaidLockData>();
		}
		public static function get i():GameDataSingleton
		{
			return _instance ||= new GameDataSingleton();
		}
		public function set builds(xml:XML):void 
		{
			for each (var item:XML in xml.build) 
			{
				var build:BaseBuild = new BaseBuild();
				build.id = item.id;
				build.title = item.title;
				build.buildType = item.buildType;
				build.upgradeId = item.upgradeId;
				build.resId = item.resId;
				build.attack = item.attack;
				build.defense = item.defense;
				build.buildIcon = item.buildIcon;
				build.attackSpeed = item.attackSpeed;
				build.attackArea = item.attackArea;
				build.areaW = item.areaW;
				build.areaH = item.areaH;
				build.cdGap = item.cdGap;
				build.areaCellW =  Math.ceil(build.areaW / Globals.cellW);
				build.areaCellH =  Math.ceil(build.areaH / Globals.cellH);
				build.flag = item.flag;
				build.describe = item.describe;
				build.upgradeGold = item.upgradeGold;
				build.buildGold = item.buildGold;
				build.buildLevel = item.buildLevel;
				build.skillIds = new Vector.<int>();
				for each (var skillId: XML in item.skillIds) 
				{
					build.skillIds.push(skillId.skillId);
				}
				_builds.push(build);
			}
		}
		public function getBaseBuildById(p_id:int):BaseBuild
		{
			var len:int = _builds.length;
			for (var j:int = 0; j < len; j++) 
			{
				if (_builds[j].id == p_id)
				{
					return _builds[j];
				}
			}
			return null;
		}
		public function getBaseBuildByType(p_type:int):BaseBuild
		{
			var len:int = _builds.length;
			for (var j:int = 0; j < len; j++) 
			{
				if (_builds[j].buildType == p_type)
				{
					return _builds[j];
				}
			}
			return null;
		}
		
		public function set skills(xml:XML):void 
		{
			for each (var item:XML in xml.skill) 
			{
				var skill:BaseSkill = new BaseSkill();
				skill.skillId = item.skillId;
				skill.title = item.title;
				skill.baseId = item.baseId;
				skill.area = item.area;
				skill.resId = item.resId;
				skill.level = item.levle;
				skill.cdGap = item.cdGap;
				skill.gravity = item.gravity;
				skill.magicType = item.magicType;
				skill.hit = item.hit;
				skill.hitId = item.hitId;
				skill.injuredId = item.injuredId;
				skill.magic = item.magic;
				skill.isAOE = Boolean(int(item.isAOE));
				skill.aoeAreaW = item.aoeAreaW;
				skill.aoeAreaH = item.aoeAreaH;
				skill.aoeAreaCellW = int(skill.aoeAreaW / Globals.cellW);
				skill.aoeAreaCellH = int(skill.aoeAreaH / Globals.cellH);
				//trace(skill.aoeAreaW, skill.aoeAreaH, skill.aoeAreaCellW, skill.aoeAreaCellH);
				skill.isPath = Boolean(int(item.isPath));
				skill.speed = item.speed;
				skill.describe = item.describe;
				skill.skillTypes = new Vector.<int>();
				for each (var skillType:XML in item.skillTypes) 
				{
					skill.skillTypes.push(int(skillType.type));
				}
				_skills.push(skill);
			}
		}
		public function getBaseSkillById(p_id:int):BaseSkill
		{
			var len:int = _skills.length;
			for (var j:int = 0; j < len; j++) 
			{
				if (_skills[j].skillId == p_id)
				{
					return _skills[j];
				}
			}
			return null;
		}
		
		public function set energys(xml:XML):void
		{
			for each (var item:XML in xml.energy)
			{
				var tempInfo:BaseRole = new BaseRole();
				tempInfo.id = item.id;
				tempInfo.title = item.title;
				tempInfo.resId = item.resId;
				tempInfo.attack = item.attack;
				tempInfo.defense = item.defense;
				tempInfo.attackSpeed = item.attackSpeed;
				tempInfo.speed = item.speed;
				tempInfo.life = item.life;
				tempInfo.attackArea = item.attackArea;
				tempInfo.describe = item.describe;
				tempInfo.loseLife = item.loseLife;
				tempInfo.gainGold = item.gainGold;
				_energys.push(tempInfo);
			}
		}
		public function getBaseRoleById(p_id:int):BaseRole
		{
			var len:int = _energys.length;
			for (var j:int = 0; j < len; j++) 
			{
				if (_energys[j].id == p_id)
				{
					return _energys[j];
				}
			}
			return null;
		}
		public function set flags(xml:XML):void 
		{
			for each (var item:XML in xml.flag) 
			{
				var flag:BaseFlag = new BaseFlag();
				flag.id = item.id;
				flag.title = item.title;
				flag.resId = item.resId;
				flag.areaW = item.areaW;
				flag.areaH = item.areaH;
				flag.areaCellW =  Math.ceil(flag.areaW / Globals.cellW);
				flag.areaCellH =  Math.ceil(flag.areaH / Globals.cellH);
				flag.flagType = item.flagType;
				flag.flagIcon = item.flagIcon;
				flag.attackArea = item.attackArea;
				flag.flagLevel = item.flagLevel;
				flag.skillIds = new Vector.<int>();
				flag.areaCellW =  Math.ceil(flag.areaW / Globals.cellW);
				flag.areaCellH =  Math.ceil(flag.areaH / Globals.cellH);
				for each (var skillId: XML in item.skillIds) 
				{
					flag.skillIds.push(skillId.skillId);
				}
				flag.describe = item.describe;
				_flags.push(flag);
			}
		}
		public function getBaseFlagById(p_id:int):BaseFlag
		{
			var len:int = _flags.length;
			for (var j:int = 0; j < len; j++) 
			{
				if (_flags[j].id == p_id)
				{
					return _flags[j];
				}
			}
			return null;
		}
		public function getBaseFlagByType(p_type:int):BaseFlag
		{
			var len:int = _flags.length;
			for (var j:int = 0; j < len; j++) 
			{
				if (_flags[j].flagType == p_type)
				{
					return _flags[j];
				}
			}
			return null;
		}
		public function getBaseFlagByTypeAndLevel(p_type:int,p_level:int):BaseFlag
		{
			var len:int = _flags.length;
			for (var j:int = 0; j < len; j++) 
			{
				if (_flags[j].flagType == p_type && _flags[j].flagLevel == p_level)
				{
					return _flags[j];
				}
			}
			return null;
		}
		
		public function set buffs(xml:XML):void
		{
			for each (var item:XML in xml.buff) 
			{
				var buff:BaseBuff = new BaseBuff();
				buff.id			  	= item.id;
				buff.baseId			= item.baseId;
				buff.icon			= item.icon;
				buff.describe    	= item.describe;
				buff.type        	= item.type;
				buff.effect      	= item.effect;
				buff.time        	= item.time;
				buff.gap			= item.gap;
				buff.magic			= item.magic;
				buff.level			= item.level;
				_buffs.push(buff);
			}
		}
		
		public function getBaseBuffById(pId:int):BaseBuff
		{
			var len:int = _buffs.length;
			for (var j:int = 0; j < len; j++) 
			{
				if (_buffs[j].id == pId)
				{
					return _buffs[j];
				}
			}
			return null;
		}
		
		
		public function setRaidButtons(xml:XML):void
		{
			for each (var item:XML in xml.button) 
			{
				var button:RaidButtonData = new RaidButtonData();
				button.x =          item.x;
				button.y =          item.y;
				button.id =         item.id;
				button.name =       String(item.name);
				button.describe =   item.describe;
				raidButtons[raidButtons.length] = button;
			}
		}
		public function getRaidButtonsById(id:int):RaidButtonData
		{
			var len:int = raidButtons.length;
			for (var j:int = 0; j < len; j++) 
			{
				if (raidButtons[j].id == id)
				{
					return raidButtons[j];
				}
			}
			return null;
		}
		
		
		public function setRaidLocks(xml:XML):void
		{
			for each (var item:XML in xml.raid) 
			{
				var raidLockData:RaidLockData = new RaidLockData();
				raidLockData.raidId = item.raidId;
				raidLockData.levelLimits = new Vector.<int>(4);
				for each(var build:XML in item.build)
				{
					raidLockData.levelLimits[int(build.type)] = int(build.levelLimit);
				}
				raidLocks[raidLocks.length] = raidLockData;
			}
		}
		
		
		
		public function getRaidLockByRaidId(raidId:int):RaidLockData
		{
			var len:int = raidLocks.length;
			for (var j:int = 0; j < len; j++) 
			{
				if (raidLocks[j].raidId == raidId)
				{
					return raidLocks[j];
				}
			}
			return null;
		}
		
	}

}